package com.liuhouse.controller;

import com.liuhouse.model.Student;
import com.liuhouse.service.StudentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DubboController {

    //公共类提供的服务
    //这里是通过远程的Dubbo服务进行提供服务的,这就就会使用dubbo对应的实现类去进行对象的实例化
    @DubboReference(interfaceClass = StudentService.class , version = "1.0")
    private StudentService studentService;

    @PostMapping("/student/add")
    public String addStudent(Student student){
        //对student的数据做处理,name需要有值,phone，age都必须有值
//        0:添加失败
////        1:添加成功
////        2:学生已经存在
////        3:传递过来的学生电话为空
        int result = studentService.addStudent(student);
        String msg = "请稍后处理";
        if(result == 1){
            msg = "添加"+student.getName()+"成功";
        }else if(result == 2){
            msg = student.getPhone()+"此手机号已经注册过了,请更换手机号";
        }else if(result == 3){
            msg = "此手机号为空";
        }else {
            msg = "添加失败";
        }
        return msg;
    }

    @GetMapping("/student/query")
    public String queryStudent(Integer id){
        String msg = "1212";
        Student student = null;
        if(id != null && id > 0){
            student = studentService.queryStudent(id);
            System.out.println(student);
            if(student != null){
                msg = "查询的学生信息 :" + student.toString();
            }else{
                msg = "没有查询到学生信息";
            }
        }else{
            msg = "查询的id不正确";
        }
        return msg;
    }

}




